CSharpTest.Net
Remove(TKey) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace > BTreeDictionary<TKey,TValue> Class > Remove Method : Remove(TKey) Method

key
The key of the element to remove.

Glossary Item Box

Removes the element with the specified key from the IDictionary.

Syntax

Visual Basic (Declaration) 
Public Overloads Function Remove( _
   ByVal key As TKey _
) As Boolean
C# 
public bool Remove( 
   TKey key
)

Parameters

key
The key of the element to remove.

Return Value

true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original IDictionary.

Exceptions

ExceptionDescription
System.NotSupportedExceptionThe IDictionary is read-only.

Example

Library/Library.Test/TestBTreeDictionary.cs

C#Copy Code
BTreeDictionary<int, string> data = new BTreeDictionary<int, string>();
data.Add(1, "a");
data.Add(2, "b");
data.Add(3, "c");
data.Add(4, "d");
data.Add(5, "e");

Assert.AreEqual(1, data.First().Key);
Assert.AreEqual("a", data.First().Value);
data.Remove(1);
Assert.AreEqual(2, data.First().Key);
Assert.AreEqual("b", data.First().Value);

Assert.AreEqual(5, data.Last().Key);
Assert.AreEqual("e", data.Last().Value);
data.Remove(5);
Assert.AreEqual(4, data.Last().Key);
Assert.AreEqual("d", data.Last().Value);

data.Remove(4);
data.Remove(3);

KeyValuePair<int, string> kv;
Assert.IsTrue(data.TryGetLast(out kv));
Assert.IsTrue(data.TryGetFirst(out kv));
data.Remove(2);
Assert.IsFalse(data.TryGetLast(out kv));
Assert.IsFalse(data.TryGetFirst(out kv));

try
{
    data.First();
    Assert.Fail("Should raise InvalidOperationException");
}
catch (InvalidOperationException)
{
}
try
{
    data.Last();
    Assert.Fail("Should raise InvalidOperationException");
}
catch (InvalidOperationException)
{
}
VB.NETCopy Code
Dim data As New BTreeDictionary(Of Integer, String)()
data.Add(1, "a")
data.Add(2, "b")
data.Add(3, "c")
data.Add(4, "d")
data.Add(5, "e")

Assert.AreEqual(1, data.First().Key)
Assert.AreEqual("a", data.First().Value)
data.Remove(1)
Assert.AreEqual(2, data.First().Key)
Assert.AreEqual("b", data.First().Value)

Assert.AreEqual(5, data.Last().Key)
Assert.AreEqual("e", data.Last().Value)
data.Remove(5)
Assert.AreEqual(4, data.Last().Key)
Assert.AreEqual("d", data.Last().Value)

data.Remove(4)
data.Remove(3)

Dim kv As KeyValuePair(Of Integer, String)
Assert.IsTrue(data.TryGetLast(kv))
Assert.IsTrue(data.TryGetFirst(kv))
data.Remove(2)
Assert.IsFalse(data.TryGetLast(kv))
Assert.IsFalse(data.TryGetFirst(kv))

Try
    data.First()
    Assert.Fail("Should raise InvalidOperationException")
Catch generatedExceptionName As InvalidOperationException
End Try
Try
    data.Last()
    Assert.Fail("Should raise InvalidOperationException")
Catch generatedExceptionName As InvalidOperationException
End Try

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys